/**
 * creater:chuanpeng.zhu
 * create time:2015/12/30
 * describe：http请求的拦截器
 */

/**
 * LOADING 拦截器
 */
APP.factory('LoadingInterceptor', ['$rootScope', '$q', function ($rootScope, $q ) {
  return {
    responseError: function (rejection) {
      //检测网络异常
      console.log('***************error!******************');
      $rootScope.$broadcast('NETWORK:ERROR', rejection);
      $rootScope.$broadcast('LOADING:HIDE');
      $q.reject(rejection);
    },
    requestError: function (response) {
      console.log('***************error!!******************');
      $rootScope.$broadcast('NETWORK:ERROR', response);
      $rootScope.$broadcast('LOADING:HIDE');
      return response
    },
    request: function (config) {
      var pattern = /http:\/\//;  //拦截http协议
      var pattern2 = /https:\/\//;  //拦截https协议
      //判断是否显示加载条，noLoading为true时不显示。
      if ((pattern.exec(config.url) || pattern2.exec(config.url)) && (!config.data || (config.data && !config.data.noLoading))) {
        //判断是否显示加载条，noLoading为true时不显示。
          $rootScope.$broadcast('LOADING:SHOW');
      }
      return config;
    },
    response: function (response) {
      var pattern = /http:\/\//;  //拦截http协议
      var pattern2 = /https:\/\//;  //拦截https协议
      if (response && (pattern.exec(response.config.url) || pattern2.exec(response.config.url))) {
        if(!response.config.noLoading&&response.config.url.indexOf('binderInfo/upLoadImage.ajax')<=-1) {
          $rootScope.$broadcast('LOADING:HIDE');//取消加载进度条
        }
      }
      return response;
    }
  }
}])

/**
 * 登录 拦截器
 */
  .factory('UserInterceptor', ['UserService', '$rootScope', 'UrlService', function (UserService, $rootScope, UrlService) {
    return {
      request: function (config) {
        if (config.method === 'GET') {
          config.url = config.url + "";
        }
        // if(!UserService.isUserLogin()&&config.method === 'POST'){
        //   $rootScope.$broadcast('REQUIRE_LOGIN');
        // }
        return config;
      },
      response: function (response) {
        //拦截如果业务表示代码code为-1，约定错误为需要登录信息。跳转到登录页面。
        if (response && response.data.code == -1 && !angular.equals(response.config.url, UrlService.getUrlData('LOGIN'))) {
          UserService.clearUser();
          $rootScope.$broadcast('REQUIRE_LOGIN');
        }
        return response;
      }
    }
  }])

  /**
   * 鉴权 拦截器
   */
  .factory('AutherInterceptor', ['UserService', '$rootScope', 'UrlService', function (UserService, $rootScope, UrlService) {
    return {
      request: function (config) {
        if (config.method === 'POST') {
            if (UserService.isUserLogin()) {
               config.data.userId = UserService.getUser().userId;
              config.data.token = UserService.getUser().token;
            }
            //config.data.userId = '76';
          }
        return config;
      }
    }
  }]);

//http拦截器注册
var interceptors = ['$httpProvider', '$ionicConfigProvider', function ($httpProvider, $ionicConfigProvider) {
  $httpProvider.interceptors.push('UserInterceptor');
  $httpProvider.interceptors.push('AutherInterceptor');
  $httpProvider.interceptors.push('LoadingInterceptor');
  $httpProvider.defaults.timeout = 15000;
}];
